package com.base.system.managedBean.baseBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.annotation.SelectItems;

import com.base.common.provider.CommonKeySelectItemProvider;
import com.base.common.provider.SelectItemsProvider;
import com.base.commonKey.entity.CommonKey;
import com.base.commonKey.entity.CommonKeyType;
import com.base.commonKey.service.ICommonKeyDao;
import com.base.commonKey.service.ICommonKeyService;
import com.base.commons.SpringBeanHolder;

public class BaseCommonKeyBean implements Serializable {
	protected ICommonKeyDao commonKeyDao = SpringBeanHolder
			.getBean("commonKeyDao");
	protected ICommonKeyService commonKeyService = SpringBeanHolder
			.getBean("commonKeyService");
	@Accessible
	protected SelectItem[] enable_items = SelectItemsProvider.getYesNo();
	@Accessible
	@SaveState
	@Bind(id = "id", attribute = "value")
	protected String id;
	@SaveState
	@Bind(id = "keyValue", attribute = "value")
	protected String keyValue;

	@SaveState
	@Bind(id = "shortCommonKeyValue", attribute = "value")
	protected String shortCommonKeyValue;

	@Bind(id = "checkEnable", attribute = "value")
	@SelectItems(source = "#{this.enable_items}")
	protected boolean checkEnable = true;
	@Bind(id = "indexNumber", attribute = "value")
	protected Integer indexNumber;
	@Bind(id = "enabled", attribute = "value")
	@SelectItems(source = "#{this.enable_items}")
	protected boolean enabled = true;
	@SaveState
	@Bind(id = "parentId", attribute = "value")
	protected String parentId;

	@Accessible
	protected SelectItem[] column_items = CommonKeySelectItemProvider
			.getCommonKey("expertColumn", false);

	@Bind(id = "columnItems", attribute = "value")
	@SelectItems(source = "#{this.column_items}")
	protected String columnItems;

	@Bind(id = "forColumn")
	protected String forColumn;

	/**
	 * 配置项类型
	 */
	@Accessible
	protected SelectItem[] keyType_items = this.getAllCommonKeyType();

	@Bind(id = "keyType")
	@SelectItems(source = "#{this.keyType_items}")
	protected String keyType;

	// 得到所有的配置项类型
	protected SelectItem[] getAllCommonKeyType() {
		List<CommonKeyType> typeList = commonKeyService.getAllCommonKeyType();
		if (typeList.isEmpty())
			return null;
		SelectItem[] result = new SelectItem[typeList.size()];
		int i = 0;
		for (CommonKeyType type : typeList) {
			result[i++] = new SelectItem(type.getTypeCode(), type.getTypeName());
		}

		return result;
	}

	@Bind(id = "typeDescription", attribute = "value")
	@SelectItems(source = "#{this.column_items}")
	protected String typeDescription;

	@Bind(id = "freeItem1", attribute = "value")
	@SelectItems(source = "#{this.free_items}")
	protected String freeItem1;

	@Accessible
	protected SelectItem[] free_items = CommonKeySelectItemProvider
			.getCommonKey("expertType", false);

	@Bind(id = "commonKey")
	@Accessible
	protected CommonKey commonKey;

	protected void setCommonKeyProperty() {
		if (commonKey == null)
			return;

		commonKey.setTypeDescription(typeDescription);
		commonKey.setCheckEnable(checkEnable);
		commonKey.setEnabled(enabled);

		List ids = new ArrayList();

		int parentIdInt = 0;
		if (!"".equals(parentId)&&parentId!=null) {
			try {
				parentIdInt = Integer.parseInt(parentId);
			} catch (Exception e) {
				parentIdInt = 0;
				e.printStackTrace();
			}
		}

		ids.add(parentIdInt);
		List<CommonKey> coms = commonKeyDao.findByIds(ids);
		CommonKey parentCommonKey = commonKeyService
				.getCommonKeyById(parentIdInt);

		commonKey.setCommonKey(parentCommonKey);
		commonKey.setIndexNumber(indexNumber);
		commonKey.setKeyType(keyType);
		commonKey.setKeyValue(keyValue);
		commonKey.setFreeItem1(freeItem1);
		commonKey.setShortCommonKeyValue(shortCommonKeyValue);
	}

	protected void getCommonKeyProperty() {
		if (commonKey == null)
			return;
		id = commonKey.getId().toString();
		typeDescription = commonKey.getTypeDescription();
		checkEnable = commonKey.isCheckEnable();
		enabled = commonKey.isEnabled();
		indexNumber = commonKey.getIndexNumber();
		keyType = commonKey.getKeyType();
		keyValue = commonKey.getKeyValue();
		freeItem1 = commonKey.getFreeItem1();
		shortCommonKeyValue = commonKey.getShortCommonKeyValue();
		if (commonKey.getCommonKey() != null) {
			parentId = commonKey.getCommonKey().getId() + "";
		}
	}
}
